package com.wuhan.template.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.google.gson.internal.LinkedTreeMap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseApplication;
import com.wuhan.template.module.find.webview.WebViewActivity;
import com.wuhan.template.module.home.activity.NewsListActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/9 0009.
 */
public class HomeAdvertisingPageAdapter extends PagerAdapter {

    private Context context;
    private List<Map<String, String>> contentBeans;
    private ImageLoader loader;
    private LayoutInflater inflater;
    private final List<ImageView> imageViews;
    private DisplayImageOptions options;
    //private List<HomeDetail.ContentBean> contentBeans;

    public HomeAdvertisingPageAdapter(Context context, List<Map<String, String>> contentBeans) {
        this.context = context;
        this.contentBeans = contentBeans;
        loader = ((BaseApplication) (this.context.getApplicationContext())).getImageLoader();
        inflater = LayoutInflater.from(context);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .showImageOnLoading(R.mipmap.app_logo)
                .build();

        imageViews = new ArrayList<>();
        for (int i = 0; i < contentBeans.size(); i++) {
            imageViews.add(new ImageView(context));
        }

    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        position %= contentBeans.size();
        if (position < 0) {
            position = contentBeans.size() + position;
        }
        final Map<String, String> map = contentBeans.get(position);
        if (contentBeans.get(position) instanceof LinkedTreeMap) {
            String imageUrl = map.get("host") + map.get("dir") + map.get("filepath") + map.get("filename");
            ImageView currentImg = imageViews.get(position);
            currentImg.setScaleType(ImageView.ScaleType.FIT_XY);
            ((BaseApplication) context.getApplicationContext())
                    .getImageLoader()
                    .displayImage(imageUrl, currentImg, options);
            ViewParent vp = currentImg.getParent();
            if (vp != null) {
                ViewGroup parent = (ViewGroup) vp;
                parent.removeView(currentImg);
            }
            container.addView(currentImg);
            switch (position) {
                case 0:
                    currentImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, NewsListActivity.class);
                            intent.putExtra("outlink", map.get("outlink"));
                            intent.putExtra("newsTitle", map.get("title"));
                            intent.putExtra("imageUrl", map.get("host") + map.get("dir") + map.get("filepath") + map.get("filename"));
                            context.startActivity(intent);
                        }
                    });
                    break;
                case 1:
                    currentImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, WebViewActivity.class);
                            intent.putExtra("url", "http://www.appwuhan.com/folder99/folder101/2016-05-11/80141.html");
                            intent.putExtra("title", "活动");
                            context.startActivity(intent);
                        }
                    });
                    break;
                case 2:
                    currentImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, NewsListActivity.class);
                            intent.putExtra("outlink", map.get("outlink"));
                            intent.putExtra("newsTitle", map.get("title"));
                            intent.putExtra("imageUrl", map.get("host") + map.get("dir") + map.get("filepath") + map.get("filename"));
                            context.startActivity(intent);
                        }
                    });
                    break;
                case 3:
                    currentImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, WebViewActivity.class);
                            intent.putExtra("url", "http://www.appwuhan.com/folder99/folder101/2016-05-11/78983.html");
                            intent.putExtra("title", "广电");
                            context.startActivity(intent);
                            context.startActivity(intent);
                        }
                    });
                    break;
            }

            return currentImg;
        }

        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
